// void, Obj This

Unit this;
Unit uEnemy;

Sleep(920+rand(GetConst("RandTimeStart"))+GetConst("AddTimeStart"));

this = This.AsUnit;

while(!.IsValid) Sleep(10000); 
//pr("piked dziala");
//if(IsAIPlayer(.player)){
	//while(.IsValid) {
		//Sleep(90000);
		//if(!IsAIPlayer(.player)) break;
	//}
		
//} else {

while(.IsValid){
        
	if(!.InHolder){
		if(.command == "stand_position" || .command == "engage" || .command == "engage_unit_type"){
			if(.stamina >= 1){
				uEnemy = .BestTargetInRange(.posRH, 70).AsUnit;
				if(uEnemy.IsAlive){
					if(.DistTo(uEnemy.pos) > .min_range){
					if(.IsValidTarget(uEnemy)){
						int l;
						
						if(uEnemy.IsHeirOf("Horse") || uEnemy.IsHeirOf("RangedHorse")){
							int power;                                
								
							//i = 0;
							if(.hero.IsValid){
								Hero h;
								// read in hero_name_favourite_and_skill.vs
								power = EnvReadInt(.hero.player, "PikedCount/"+.hero.name);
							} 
							power = power+(.armor_slash*4); 
							.AddBonus(power,0,0,0,0);
							.SetStamina(.stamina - 1);
							Sleep(4000);
							if(.IsValid)
								.RemoveBonus(power,0,0,0,0);
							else
								break;
						} else {
							while(.command == "engage" || .command == "engage_unit_type" || (!uEnemy.IsHeirOf("Horse") && !uEnemy.IsHeirOf("RangedHorse"))){
								Sleep(1000);
								if(!.IsValid) break;
								l+=1;
								if(l > 40) break;
							}
						} 
					}
					}
				} else { Sleep(1000); }
			
			} else { Sleep(950+rand(100)); }
		} else { Sleep(4000); }
	} else { Sleep(4000); }
	Sleep(900+rand(200));	
}
//}
